const WXAPI = require('../../wxapi/main')

Page({
    data: {
        showUp: false,
        xx: '',
        heatCount: 8444,
        likeCount: 1056,
        albumTitle: '英伦证件照',
        isFavorited: false // 初始状态为未收藏
    },

    onLoad: function (e) {
        const that = this;
        // 设置当前分类 ID
        if (e && e.id) {
            this.setData({ id: e.id });
        }

        // 初始化加载第一页数据
        this.getDetail();
        WXAPI.goodsFavCheckV2({ goodsId: this.data.id, type: 0, token: wx.getStorageSync("token") }).then(res => {
            if (res.code == -1) {
                this.setData({
                    isFavorited: false
                })
            } else {
                this.setData({
                    isFavorited: true
                })
            }
            // that.setData({ xx: res.data });
            console.log(res.data);

        }).catch(() => { });

    },
    // /shop/goods/fav/list/v2
    // goodsFavDeleteV2
    toggleFavorite: function () {

        if (this.data.isFavorited == false) {
            WXAPI.goodsFavAdd({ goodsId: this.data.id, type: 0, token: wx.getStorageSync("token") }).then(res => {
                const isFavorited = !this.data.isFavorited;

                this.setData({
                    isFavorited: isFavorited
                });

 this.getDetail();

                if (isFavorited) {
                    wx.showToast({
                        title: '收藏成功',
                        icon: 'success'
                    });
                } else {
                    wx.showToast({
                        title: '已取消收藏',
                        icon: 'none'
                    });
                }
            })
        } else {
             WXAPI.goodsFavDeleteV2({ goodsId: this.data.id, type: 0, token: wx.getStorageSync("token") }).then(res => {
                const isFavorited = !this.data.isFavorited;

                this.setData({
                    isFavorited: isFavorited
                });


 this.getDetail();
                if (isFavorited) {
                    wx.showToast({
                        title: '收藏成功',
                        icon: 'success'
                    });
                } else {
                    wx.showToast({
                        title: '已取消收藏',
                        icon: 'none'
                    });
                }
            })

        }
       

    },

    getDetail() {
        const that = this;
        WXAPI.goodsDetailV2({ id: this.data.id }).then(res => {
            that.setData({ xx: res.data });
            console.log(res.data);

        }).catch(() => { });
    },

    // 页面滚动监听
    onPageScroll(e) {
        const scrollTop = e.scrollTop;

        // 显示/隐藏回到顶部按钮
        this.setData({ scrollTop, showUp: scrollTop > 100 });


        const query = wx.createSelectorQuery();
        query.select('.container').boundingClientRect(res => {
            if (res) {
                const scrollBottom = res.height - (scrollTop);
                if (scrollBottom <= 200) {
                    this.loadMore();
                }
            }
        }).exec();
    },


    // 回到顶部
    up() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        });
    },

    // 分享
    onShareAppMessage() {
        return {
            title: "GOOD STUDIO儿童摄影",
            path: "/pages/index/detail?id=" + this.data.id
        };
    },

    onShareTimeline() {
        return {
            title: "GOOD STUDIO儿童摄影"
        };
    },

    // 侧栏功能
    tel() {
        wx.makePhoneCall({
            phoneNumber: wx.getStorageSync('shopSubdetail')?.info?.linkPhone || ''
        });
    },

    sc() {
        wx.navigateTo({
            url: '/pages/index/sc'
        });
    },

    golist(e) {
        wx.navigateTo({
            url: '/pages/index/list?id=' + e.currentTarget.dataset.id
        });
    },



    //点击图片放大,幻灯片
    fd: function (e) {
        var t = e.currentTarget.dataset.src;
        wx.previewImage({
            current: t, // 当前显示图片的http链接
            urls: this.data.xx.pics2 // 需要预览的图片http链接列表
        });
    },

});